package app.frames;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import libs.Attendance;
import libs.AttendanceEntry;
import libs.AttendanceException;
import libs.AttendanceImplDb;
import libs.Seminar;
import libs.Student;

/**
 *  Table model representing student's attendance
 * @author Dejvino
 */
public class AttendanceTableModel extends DefaultTableModel{

    private Attendance attendance;
    private Student student;
    private Seminar seminar;

    public AttendanceTableModel(){
        this(null, null);
    }

    public AttendanceTableModel(Attendance attendance){
        this(attendance, null, null);
    }

    public AttendanceTableModel(Student student, Seminar seminar){
        super();
        this.student = student;
        this.seminar = seminar;
        this.attendance = new AttendanceImplDb();
    }

    public AttendanceTableModel(Attendance attendance, Student student, Seminar seminar){
        super();
        if(attendance == null)
            throw new NullPointerException("attendance");
        this.student = student;
        this.seminar = seminar;
        this.attendance = attendance;
    }

    public Seminar getSeminar() {
        return seminar;
    }

    public void setSeminar(Seminar seminar) {
        this.seminar = seminar;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public int getColumnCount(){
        return 2;
    }

    @Override
    public String getColumnName(int column){
        switch(column){
            case 0:
                return java.util.ResourceBundle.getBundle("app/frames/language").getString("Date");
            case 1:
                return java.util.ResourceBundle.getBundle("app/frames/language").getString("Attendance");
            default:
                throw new IllegalArgumentException("column");
        }
    }

    @Override
    public int getRowCount(){
        if(attendance == null || seminar == null)
            return 0;
        Map<Student, List<AttendanceEntry>> map = attendance.getAttendance(seminar);
        if(map == null)
            return 0;
        List<AttendanceEntry> list = map.get(student);
        if(list == null)
            return 0;
        return list.size();
    }

    @Override
    public Object getValueAt(int row, int column) {
        AttendanceEntry entry = (AttendanceEntry)attendance.getAttendance(seminar).get(student).get(row);
        if(column == 0)
            return entry.getDate();
        if(column == 1)
            return Boolean.valueOf(entry.isPresent());
        throw new IllegalArgumentException("column");
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return Date.class;
            case 1:
                return Boolean.class;
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        AttendanceEntry entry = (AttendanceEntry)attendance.getAttendance(seminar).get(student).get(rowIndex);
        switch (columnIndex) {
            case 0:
                entry.setDate((Date)aValue);
                break;
            case 1:
                entry.setPresent((Boolean)aValue);
                break;
            default:
                throw new IllegalArgumentException("columnIndex");
        }
        try {
            attendance.removeAttendance(seminar, student, entry);
            attendance.addAttendance(seminar, student, entry);
        } catch (AttendanceException ex) {
            Logger.getLogger(AttendanceTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        fireTableCellUpdated(rowIndex, columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
            case 1:
                return true;
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }
}
